<?

class pages {
   
   public $error;
   
   function pages (){
      global $logger;
	  $this->logger = $logger;
      return $this;
   }

   public function getTopic($id){
	    //$this->topicId = $this->getTopicIdByName($topicName);
		  $this->topicId = $id;
		  $this->articles = $this->getAllArticles();
		return $this->articles;
   }
   
   public function getArticle($artId){
	    $this->article = $this->setQuotes($this->getArticleById($artId));
		
		return $this->article;
   }
   
   private function getTopicIdByName($topicName){
	  $topics = array('news' => 2);
	  	  
	  if(array_key_exists($topicName,$topics)){
	     return $topics[$topicName];
	  } else {
	    $this->error("No page by this name");	    
	  }
   }   
   
   private function getAllArticles(){
      global $tables, $mdb2, $dsn;
	  
      $articles = new ArticlesTable($dsn);
	  $articles->reset();
	  $articles->setWhere('art_page_id='.$this->topicId." AND art_status=1 ORDER BY art_date DESC");
	  
	  $articles->setLeftJoin($tables['pages'], $tables['articles'].".art_page_id=".$tables['pages'].".page_id");
	  $ret = $articles->getAll();
	 return $ret;	   	  
   }
  
   public function getPath(){
      global $logger;
	  if(!empty($this->artId)) {
	     $path[] = array($this->article[0]['art_id'], $this->article[0]['art_title']);
	     $path[] = array($this->article[0]['_pages_page_id'], $this->article[0]['_pages_page_title']);  
	  } elseif(!empty($this->topicId)) {
	     $path[] = array( $this->articles[0]['_pages_page_id'], $this->articles[0]['_pages_page_title']);
	  }
	  
	  $logger->log("PATH :".print_r($path, true));
    return $path;
   }

# private functions  
   private function getTopicByArtId($artId){
     global $tables, $mdb2, $dsn;
      $article = new ArticlesTable($dsn);
	  $article->reset();
	  $article->setWhere('art_id='.$artId." AND art_status=1");
	  $article->setLeftJoin($tables['pages'], $tables['articles'].".art_page_id=".$tables['pages'].".page_id");
	  $ret = $article->getAll();
	  return  $ret[0]['_pages_page_id'];
   }
    
   private function getArticleById($artId){
      global $tables, $mdb2, $dsn;
	  $this->artId = $artId;
      $article = new ArticlesTable($dsn);
	  $article->reset();
	  $article->setWhere('art_id='.$artId." AND art_status=1");
	  $article->setLeftJoin($tables['pages'], $tables['articles'].".art_page_id=".$tables['pages'].".page_id");
	  $ret = $article->getAll();
	  
   return $ret;	   	  
   }
   
   private function error($errMsg){
      global $logger;
	  $logger->log($errMsg, PEAR_LOG_DEBUG);
	  $this->error = $errMsg;
	  throw new Exception($this->error);
   }
   
   private function setQuotes($array){
      foreach ($array as &$arrayItem){
	    array_walk($arrayItem, 'set_quotes');	  
	  }
	  return $array;
   }
   
     private function escQuotes($array){
      foreach ($array as &$arrayItem){
	    array_walk($arrayItem, 'esc_quotes');	  
	  }
	  return $array;
   }
   
}

?>